package com.athuiju.hr.controller;


import com.athuiju.commonutils.R;
import com.athuiju.hr.entity.Performance;
import com.athuiju.hr.entity.Profiles;
import com.athuiju.hr.entity.Sector;
import com.athuiju.hr.entity.vo.PerBilityVo;
import com.athuiju.hr.entity.vo.PerIdeaVo;
import com.athuiju.hr.entity.vo.PerformanceVo;
import com.athuiju.hr.entity.vo.PerformanceVoQuery;
import com.athuiju.hr.service.PerformanceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wzh
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/hr/performance")
@Api(value="绩效管理controller",tags={"绩效管理操作接口"})
public class PerformanceController {
    @Autowired
    PerformanceService performanceService;



    @ApiOperation(value = "根据员工id查询员工绩效controller")
    @GetMapping("getPerById/{id}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = PerformanceVo.class),
    })
    public R getByPerId(@ApiParam(name = "id",value = "id",required = true)
                     @PathVariable String id) {
        List<PerformanceVo> list = performanceService.getByProId(id);
//        封装工作理念
//        封装工作能力
        return R.ok().data("PerformanceList", list);
    }
    @ApiOperation(value = "根据员工id查询员工工作理念绩效controller")
    @GetMapping("getPerIdeaById/{id}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = PerIdeaVo.class),
    })
    public R getPerIdeaById(@ApiParam(name = "id",value = "id",required = true)
                        @PathVariable String id) {
        List<PerIdeaVo> list = performanceService.getPerIdeaById(id);
        return R.ok().data("PerformanceIdeaList", list);
    }


    @ApiOperation(value = "根据员工id查询员工工作能力绩效controller")
    @GetMapping("getPerBilityById/{id}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = PerIdeaVo.class),
    })
    public R getPerBilityById(@ApiParam(name = "id",value = "id",required = true)
                            @PathVariable String id) {
        List<PerBilityVo> list = performanceService.getPerBilityById(id);
        return R.ok().data("PerformanceBilityList", list);
    }



    @ApiOperation(value = "添加绩效controller")
    @PostMapping("addPerformance")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = Performance.class),})
    public R addPerformance(@ApiParam(name = "performance", value = "人员对象", required = true)
                          @RequestBody Performance performance) {
        boolean save = performanceService.save(performance);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @ApiOperation(value = "删除绩效controller")
    @DeleteMapping("removePerById/{id}")
    public R removePerById(
            @ApiParam(name = "id", value = "绩效id", required = true)
            @PathVariable String id) {
        boolean flag = performanceService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }



    @ApiParam(value = "查询相同绩效工作理念信息")
    @ApiOperation(value = "查询相同绩效工作理念信息")
    @GetMapping("getPerformance/{profilesid}/{yearIdea}/{quarter}")
    public R getPerformance(@PathVariable(name = "profilesid")String profilesid,
                          @PathVariable(name = "yearIdea")String yearIdea,
                          @PathVariable(name = "quarter") Integer quarter
    ) {
        QueryWrapper<Performance> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("performance.profilesid",profilesid);
        queryWrapper.eq("performance.year_idea",yearIdea);
        queryWrapper.eq("performance.quarter",quarter);
        Performance one = performanceService.getOne(queryWrapper);
        if (one!=null) {
            return R.ok().data("flag",true);
        }
        return R.ok().data("flag",false);
    }

    @ApiParam(value = "查询相同绩效工作能力信息")
    @ApiOperation(value = "查询相同绩效工作能力信息")
    @GetMapping("addPerformanceAbility/{profilesid}/{yearScarce}/{quarterScarce}")
    public R addPerformanceAbility(@PathVariable(name = "profilesid")String profilesid,
                            @PathVariable(name = "yearScarce")String yearScarce,
                            @PathVariable(name = "quarterScarce") Integer quarterScarce
    ) {
        QueryWrapper<Performance> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("performance.profilesid",profilesid);
        queryWrapper.eq("performance.year_scarce",yearScarce);
        queryWrapper.eq("performance.quarter_scarce",quarterScarce);
        Performance one = performanceService.getOne(queryWrapper);

        if (one!=null) {
            return R.ok().data("flag",true);
        }
        return R.ok().data("flag",false);
    }



    @ApiOperation(value = "绩效总览分页controller")
    @PostMapping("conditionPer/{page}/{limit}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = PerformanceVo.class),
    })
    public R pageQueryConditionPer2(@ApiParam(name = "page", value = "当前页码", required = true)
                                   @PathVariable Long page,
                                   @ApiParam(name = "limit", value = "每页记录数", required = true)
                                   @PathVariable Long limit,
                                   @ApiParam(name = "PerformanceVoQuery", value = "条件查询对象", required = false)
                                   @RequestBody PerformanceVoQuery performanceVoQuery) {
        //创建page对象
        Page<PerformanceVo> pageSture = new Page(page, limit);
        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有的数据封装到pageSture里面
        List<PerformanceVo> performanceVos = performanceService.pageQuery(pageSture, performanceVoQuery);
        //取出数据封装
        //总记录数
        long total = pageSture.getTotal();
        //数据集合
        List<PerformanceVo> records = pageSture.getRecords();
        //页码大于1，且records为空或者records为0的时候，就让数据默认显示在第一页
        if(page>1){
            if(records==null || records.size()==0){
                Page<PerformanceVo>pageSture02=new Page(1,limit);
                performanceService.pageQuery(pageSture02,performanceVoQuery);
                records=pageSture02.getRecords();
            }
        }
        return R.ok().data("total", total).data("page",page).data("limit",limit).data("rows", performanceVos);
    }
    @ApiParam(value = "删除工作能力绩效controller")
    @ApiOperation(value = "删除工作能力绩效controller")
    @PostMapping("updatePerformance/{id}")
    public R updatePerformance(
            @ApiParam(name = "id", value = "绩效id", required = true)
            @PathVariable String id) {
        boolean flag = performanceService.updatePerById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiParam(value = "删除工作理念绩效controller")
    @ApiOperation(value = "删除工作理念绩效controller")
    @PostMapping("updatePerformanceIdea/{id}")
    public R updatePerformanceIdea(
            @ApiParam(name = "id", value = "绩效id", required = true)
            @PathVariable String id) {
        boolean flag = performanceService.updatePerIdeaById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiParam(value = "添加员工绩效controller")
    @ApiOperation(value = "添加员工绩效controller")
    @PostMapping("updatePer")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = Performance.class),
    })
    public R updatePer(
            @ApiParam(name = "hr", value = "人员对象", required = true)
            @RequestBody Performance performance) {
        boolean flag = performanceService.updateById(performance);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }



}

